package com.modifiertech.lineage.api;

import com.modifiertech.lineage.LineageRegistries;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class PlayerDataSkills {
    //玩家【普通】类别技能 mapping 映射
    private ConcurrentHashMap<String,PlayerDataSkill> ordinarySkillMapping=new ConcurrentHashMap<>();

    //玩家【特殊】类别技能 mapping 映射
    private ConcurrentHashMap<String,PlayerDataSkill> specialSkillMapping=new ConcurrentHashMap<>();

    //全用户技能数据
    private List<PlayerDataSkill> fullPlayerDataSkill;

    public PlayerDataSkills(){
        List<PlayerDataSkill> init = new ArrayList<>();
        for (PlayerDataSkill skill : LineageRegistries.SKILLS) {
            //数据拷贝注册全初始化技能数据
            PlayerDataSkill temp = PlayerDataSkill.copy(skill);
            init.add(temp);
        }
        fullPlayerDataSkill = init;
    }


    //当前总技能点数
    private int totalSkillPoint=0;
    //当前未分配技能点数
    private int undistributedSkillPoint=0;

    public ConcurrentHashMap<String, PlayerDataSkill> getOrdinarySkillMapping() {
        return ordinarySkillMapping;
    }

    public void setOrdinarySkillMapping(ConcurrentHashMap<String, PlayerDataSkill> ordinarySkillMapping) {
        this.ordinarySkillMapping = ordinarySkillMapping;
    }

    public ConcurrentHashMap<String, PlayerDataSkill> getSpecialSkillMapping() {
        return specialSkillMapping;
    }

    public void setSpecialSkillMapping(ConcurrentHashMap<String, PlayerDataSkill> specialSkillMapping) {
        this.specialSkillMapping = specialSkillMapping;
    }

    public int getTotalSkillPoint() {
        return totalSkillPoint;
    }

    public void setTotalSkillPoint(int totalSkillPoint) {
        this.totalSkillPoint = totalSkillPoint;
    }

    public int getUndistributedSkillPoint() {
        return undistributedSkillPoint;
    }

    public void setUndistributedSkillPoint(int undistributedSkillPoint) {
        this.undistributedSkillPoint = undistributedSkillPoint;
    }

    public void forEachEventHandler(Consumer<IAbilityEventHandler> consumer) {
        ordinarySkillMapping.values().forEach(u -> {
            if ( u.isEnable()&&u instanceof IAbilityEventHandler) {
                consumer.accept(u);
            }
        });
        specialSkillMapping.values().forEach(u -> {
            if ( u.isEnable()&&u instanceof IAbilityEventHandler) {
                consumer.accept(u);
            }
        });
    }

    /**按指定技能类型进行过滤执行
     * 确保特定技能类消费响应指定事件
     *
     * @param consumer
     * @param skillType
     */
    public void forEachEventHandlerWithType(Consumer<IAbilityEventHandler> consumer,String skillType) {
        ordinarySkillMapping.values().stream()
                .filter(h->h.getSkillName().equals(skillType))
                .forEach(u -> {
            if ( u.isEnable()&&u instanceof IAbilityEventHandler) {
                consumer.accept(u);
            }
        });
        specialSkillMapping.values().stream()
                .filter(h->h.getSkillName().equals(skillType)).
                forEach(u -> {
            if ( u.isEnable()&&u instanceof IAbilityEventHandler) {
                consumer.accept(u);
            }
        });
    }

    /**获取人物技能实例
     *
     * @param skillName
     * @return
     */
    public Optional<PlayerDataSkill> getSkillInstance(String skillName){
       Optional<String> choiseSkillName=Optional.empty();
        choiseSkillName= ordinarySkillMapping.keySet().stream().filter(
                h-> skillName.equals(h)).findFirst();
        if (choiseSkillName.isPresent()){
            return Optional.of(ordinarySkillMapping.get(choiseSkillName.get()));
        }
        //
        choiseSkillName= specialSkillMapping.keySet().stream().filter(
                h-> skillName.equals(h)).findFirst();
        if (choiseSkillName.isPresent()){
            return Optional.of(specialSkillMapping.get(choiseSkillName.get()));
        }else{
            return Optional.empty();
        }
    }

    /**获取人物技能实例
     *
     * @param skillName
     * @return
     */
    public Optional<PlayerDataSkill> getSkillInstanceFromAll(String skillName){
        Optional<PlayerDataSkill>
        choiseSkill=
                fullPlayerDataSkill.stream().filter(h->skillName.equals(h.defineSkillName())).findFirst();
        return choiseSkill;
    }

    public List<PlayerDataSkill> getFullPlayerDataSkill() {
        return fullPlayerDataSkill;
    }
}
